﻿using System;
using System.Net.Http;
using System.Text;
using Microsoft.Data.Sqlite;
using Newtonsoft.Json;
using System.Collections.Generic;
using Dapper;
using System.Linq;
using CsQuery;
using Jint;
using static System.Console;
namespace dataget
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;
            string x = ".cha test";
            Console.WriteLine(x.IndexOf(".cha "));
            //var req = new HttpClient();
            //var txt = req.GetStringAsync("http://ng.d.cn/newdb/xjcsshyhda/gem_45150.html").Result;
            //CQ cq = txt;
            //var js = cq["script:last"];
            //var json = js.Text().Substring(10);
            //WriteLine(json);
            //var L= getitemlist("http://ng.d.cn/newdb/xjcsshyhda/equip.html");
            //getallitem(ref L);
            //using (SqliteConnection sc = new SqliteConnection("DataSource=item.db"))
            //{
            //    sc.Execute("insert into item values (@id,@imgUrl,@name,@type,@get,@price,@sell,@title,@useDj)", L);
            //}
        }
        static List<itemModel> getitemlist(string url)
        {
            var req = new HttpClient();
            var txt = req.GetStringAsync(url).Result;
            CQ cq = txt;
            var jstxt = cq["script:last"].Text();
            Engine js = new Engine();
            js.Execute("var vm={};");
            js.Execute(jstxt);
            dynamic obj = js.GetValue("vm").ToObject();
            List<itemModel> rel = new List<itemModel>();
            foreach(var a in obj.equip) {
                Console.WriteLine(a.id);
                itemModel _ = new itemModel { id = (int)a.id, imgUrl = a.imgUrl, type = a.type, name = a.name };
                rel.Add(_);
            }
            
            return rel;
        }
        static void getallitem(ref List<itemModel> l)
        {
            var req = new HttpClient();
            Engine js = new Engine();
            for (int i = 0; i < l.Count; i++)
            {
                var txt = req.GetStringAsync($"http://ng.d.cn/newdb/xjcsshyhda/gem_{l[i].id}.html").Result;
                CQ cq = txt;
                var jstxt = cq["script:last"].Text();
                js.Execute("var vm={};");
                js.Execute(jstxt);
                dynamic obj = js.GetValue("vm").ToObject();
                cq = obj.gem.get;
                var temp = cq["a"].ToList();
                List<string> ls = new List<string>();
                temp.ForEach(x => { ls.Add(x.InnerText); });
                l[i].get = JsonConvert.SerializeObject(ls);
                l[i].price = Convert.ToInt32(obj.gem.price);
                l[i].sell = 0;
                l[i].title = obj.gem.title;
                l[i].useDj = Convert.ToInt32(obj.gem.useDj);
            }
        }
        
    }
}